package com.rxh.controller;

import com.rxh.pojo.core.CoreAcquirer;
import com.rxh.pojo.core.CoreAcquirerAccount;
import com.rxh.pojo.core.CoreAcquirerRate;
import com.rxh.pojo.core.CoreBank;
import com.rxh.service.AcquirerService;
import com.rxh.service.ConstantService;
import com.rxh.spring.annotation.SystemLogInfo;
import com.rxh.util.UserInfoUtils;
import com.rxh.utils.SystemConstant;
import com.rxh.vo.VoCoreAcquirer;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/3/22
 * Time: 15:13
 * Project: Management
 * Package: com.rxh.controller
 */
@Controller
@RequestMapping(value = "/acquirer")
public class AcquirerController {
    @Resource
    private AcquirerService acquirerService;

    @Resource
    private ConstantService constantService;

    /**
     * 获取所有收单银行
     *
     * @return 银行对象集合
     */
    @SystemLogInfo(description = "获取银行列表")
    @RequestMapping(value = "/bank/getAll")
    @ResponseBody
    public List<CoreBank> getAllBank() {
        return acquirerService.selectAllBank();
    }

    /**
     * 更新银行对象
     *
     * @param bank 银行对象
     * @return 是否更新成功
     */
    @SystemLogInfo(description = "更新银行对象")
    @RequestMapping(value = "/bank/update")
    @ResponseBody
    public Boolean updateBank(@RequestBody CoreBank bank) {
        bank.setModifier(SecurityContextHolder.getContext().getAuthentication().getName());
        return acquirerService.updateBank(bank, UserInfoUtils.getName());
    }

    @SystemLogInfo(description = "添加银行")
    @RequestMapping(value = "/bank/add")
    @ResponseBody
    public Boolean addBank(@RequestBody CoreBank bank) {
        bank.setCreator(SecurityContextHolder.getContext().getAuthentication().getName());
        return acquirerService.addBank(bank, UserInfoUtils.getName());
    }

    @SystemLogInfo(description = "删除银行")
    @RequestMapping(value = "/bank/delete")
    @ResponseBody
    public List<Integer> deleteBank(@RequestBody List<Integer> idList) {
        return acquirerService.deleteBank(idList);
    }

    @SystemLogInfo(description = "获取收单机构列表")
    @RequestMapping(value = "/getAllAcquirer")
    @ResponseBody
    public List<CoreAcquirer> getAllAcquirer() {
        return acquirerService.selectAllAcquirer();
    }

    @RequestMapping(value = "/getAcquirerInit")
    @ResponseBody
    public Map<String, Object> getAcquirerInit() {
        Map<String, Object> init = new HashMap<>();
        init.put("currency", constantService.getConstantByGroupNameAndSortValueIsNotNULL(SystemConstant.CURRENCY));
        init.put("bank", acquirerService.selectAllBank());
        init.put("continent", constantService.getConstantByGroupName(SystemConstant.CONTINENT));
        init.put("acquirerType", constantService.getConstantByGroupName(SystemConstant.ACQUIRER_TYPE));
        init.put("payMode", constantService.getConstantByGroupName(SystemConstant.PAY_MODE));
        init.put("payPage", constantService.getConstantByGroupName(SystemConstant.PAY_PAGE));
        init.put("risk", constantService.getConstantByGroupName(SystemConstant.RISK));
        init.put("status", constantService.getConstantByGroupNameAndSortValueIsNotNULL(SystemConstant.STATUS));
        init.put("scope", constantService.getConstantByGroupName(SystemConstant.STATUS));
        init.put("Credit", constantService.getConstantByGroupName(SystemConstant.CREDIT));
        init.put("Debit", constantService.getConstantByGroupName(SystemConstant.DEBIT));
        init.put("Yandex", constantService.getConstantByGroupName(SystemConstant.YANDEX));
        init.put("Qiwi", constantService.getConstantByGroupName(SystemConstant.QIWI));
        init.put("Remittance", constantService.getConstantByGroupName(SystemConstant.REMITTANCE));
        return init;
    }

    @SystemLogInfo(description = "更新收单机构")
    @RequestMapping(value = "/updateAcquirer")
    @ResponseBody
    public Boolean updateAcquirer(@RequestBody VoCoreAcquirer acquirer) {
        return acquirerService.updateAcquirer(acquirer, UserInfoUtils.getName());
    }

    @SystemLogInfo(description = "添加收单机构")
    @RequestMapping(value = "/addAcquirer")
    @ResponseBody
    public Boolean addAcquirer(@RequestBody VoCoreAcquirer acquirer) {
        return acquirerService.addAcquirer(acquirer, UserInfoUtils.getName());
    }

    @SystemLogInfo(description = "删除收单机构")
    @RequestMapping(value = "/deleteAcquirer")
    @ResponseBody
    public List<Integer> deleteAcquirer(@RequestBody List<Integer> idList) {
        return acquirerService.deleteAcquirer(idList);
    }

    @RequestMapping(value = "/getAcquirerRateByAcquirerId")
    @ResponseBody
    public List<CoreAcquirerRate> getAcquirerRateByAcquirerId(@RequestBody Long acquirerId) {
        return acquirerService.selectAcquirerRateByAcquirerId(acquirerId);
    }

    @RequestMapping(value = "/account/getInit")
    @ResponseBody
    public Map<String, Object> getAcquirerAccountInit() {
        Map<String, Object> init = new HashMap<>();
        init.put("acquirer", acquirerService.selectAllAcquirer());
        init.put("currency", constantService.getConstantByGroupNameAndSortValueIsNotNULL(SystemConstant.CURRENCY));
        init.put("status", constantService.getConstantByGroupNameAndSortValueIsNotNULL(SystemConstant.STATUS));
        init.put("currencyType", constantService.getConstantByGroupName(SystemConstant.CURRENCY_TYPE));
        return init;
    }

    @SystemLogInfo(description = "获取收单账户列表")
    @RequestMapping(value = "/account/getAll")
    @ResponseBody
    public List<CoreAcquirerAccount> getAllAcquirerAccount() {
        return acquirerService.selectAllAcquirerAccount();
    }

    @SystemLogInfo(description = "更新收单账户")
    @RequestMapping(value = "/account/update")
    @ResponseBody
    public Boolean updateAcquirerAccount(@RequestBody CoreAcquirerAccount acquirerAccount) {
        return acquirerService.updateAcquirerAccount(acquirerAccount, UserInfoUtils.getName());
    }

    @SystemLogInfo(description = "添加收单账户")
    @RequestMapping(value = "/account/add")
    @ResponseBody
    public Boolean addAcquirerAccount(@RequestBody CoreAcquirerAccount acquirerAccount) {
        return acquirerService.addAcquirerAccount(acquirerAccount, UserInfoUtils.getName());
    }

    @SystemLogInfo(description = "删除收单账户")
    @RequestMapping(value = "/account/delete")
    @ResponseBody
    public List<Integer> deleteAcquirerAccount(@RequestBody List<Integer> idList) {
        return acquirerService.deleteAcquirerAccount(idList);
    }
}